#include "QBackground.h"

QBackground::QBackground(void)
{
	tree = NULL;
	_myObject = NULL;
	_nodeOfTree = NULL;
	_listTile = NULL;
}

QBackground::QBackground(int level)
{
	string fileName;
	switch(level)
	{
	case 1:
		fileName = "Resources\\Maps\\stage1.txt";
		break;
	case 2:
		fileName = "Resources\\Maps\\stage2.txt";
		break;
	case 3:
		fileName = "Resources\\Maps\\stage3.txt";
		break;
	}

	ifstream map (fileName);

	_listTile = new list<Tile*>();

	if(map.is_open())
	{		
		float posX, posY; int value;
		int count;
		map>> count;
		switch(level)
		{
		case 1:
			bgSprite = new CSprite(new CTexture("Resources\\Maps\\stage1.png", count, 1, count), 1000);
			break;
		case 2:
			bgSprite = new CSprite(new CTexture("Resources\\Maps\\stage2.png", count, 1, count), 1000);
			break;
		case 3:
			bgSprite = new CSprite(new CTexture("Resources\\Maps\\stage3.png", count, 1, count), 1000);
			break;
		}
		map>> count >> G_MapWidth >> G_MapHeight;
		int id; 
		_myObject = new std::map<int, Tile*>();
		Tile* _obj;
		for (int i = 0; i < count; i++)
		{
			//so thu tu dong - idObj -...
			map>> id >> value >> posX >> posY;		

			_myObject->insert(pair<int, Tile*>(id, new Tile(value, posX, posY)));
		}

		//Doc quadtree
		string line;
		_nodeOfTree = new std::map<int, QNode*>();
		int size;
		while(!map.eof())  
		{
			//posX == left; posY == top; size == size q
			map>> id >> posX >> posY >> size;

			//Doc id Object trong node
			getline(map, line);
			istringstream str_line(line);			
			std::map<int, Tile*> *_objOfNode = new std::map<int, Tile*>();
			while(str_line >> value)
			{
				_objOfNode->insert(pair<int, Tile*>(value, _myObject->find(value)->second));
			}

			QNode* _nodeTree = new QNode(posX, posY, size, *_objOfNode);

			//Dua node vao _myMap
			_nodeOfTree->insert(pair<int, QNode*>(id, _nodeTree));
		}
		_myObject->clear();
		map.close();
	}
}

void QBackground::GetTreeObject(int viewportX, int viewportY)
{
	_listTile->clear();

	GetNodeObject(viewportX, viewportY, tree);
}

void QBackground::GetNodeObject(int viewportX, int viewportY, QNode* _node)
{
	if(_node->leftTop != NULL)
	{
		if(viewportX < _node->rightTop->left)
		{
			if(viewportY < _node->leftBottom->top)
			{
				GetNodeObject(viewportX, viewportY, _node->leftTop);
			}
			if(tree->size - (viewportY - G_ScreenHeight) > _node->leftBottom->top)
			{
				GetNodeObject(viewportX, viewportY, _node->leftBottom);
			}
		}
		if(viewportX + G_ScreenWidth > _node->rightTop->left)
		{
			if(viewportY < _node->leftBottom->top)
			{
				GetNodeObject(viewportX, viewportY, _node->rightTop);
			}
			if(tree->size - (viewportY - G_ScreenHeight) > _node->leftBottom->top)
			{
				GetNodeObject(viewportX, viewportY, _node->rightBottom);
			}
		}
	}
	else
	{
		for (map<int, Tile*>::iterator _itBegin = _node->listObject.begin(); _itBegin!=_node->listObject.end(); _itBegin++) 
		{
			_listTile->push_back(_itBegin->second);
		}
	}
}

void QBackground::LoadTree()
{
	Load(0, tree);
}

void QBackground::Load(int id, QNode *& _nodeTree)
{
	map<int, QNode*>::iterator _node = _nodeOfTree->find(id);
	if(_node != _nodeOfTree->end())
	{
		_nodeTree = new QNode(_node->second->left, _node->second->top, _node->second->size, _node->second->listObject);
		Load(_node->first*8 + 1, _nodeTree->leftTop);
		Load(_node->first*8 + 2, _nodeTree->rightTop);
		Load(_node->first*8 + 3, _nodeTree->leftBottom);
		Load(_node->first*8 + 4, _nodeTree->rightBottom);
	}
}

void QBackground::Draw(CCamera *camera)
{
	for (list<Tile*>::iterator _itBegin = _listTile->begin(); _itBegin!=_listTile->end(); _itBegin++) 
	{ 
		D3DXVECTOR2 t = camera->Transform((*_itBegin)->posX, (*_itBegin)->posY);
		bgSprite->DrawIndex((*_itBegin)->idTile, t.x, t.y);
	}
}

QBackground::~QBackground(void)
{
}